﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;
using System.Threading;
using JN.Utility;
using System.IO.Ports;
using Power.Common;

namespace Power.MeterControl
{
    public class MeterSimulator:IMeter
    {
        #region property
        private Thread readThread;
        private object lockRead = new object();
        private Thread doProcessThread;
        private object lockDoProcess = new object();
        private Manager.SystemStatus sysStatus = Manager.SystemStatus.Instance;
        private List<SerialPort> listCom = new List<SerialPort>();   //用于记录所有的serialport
        private List<Meter_Str> listTempData = new List<Meter_Str>();  //用于记录临时数据

        #endregion
        private static MeterSimulator instance = null;
        private static readonly object plcLock = new object();
        public static MeterSimulator Instance
        {
            get
            {
                lock (plcLock)
                {
                    if (instance == null)
                    {
                        instance = new MeterSimulator();
                    }
                    return instance;
                }
            }
        }
        private void StartThread()
        {
            if (readThread == null)
            {
                readThread = new Thread(new ThreadStart(ReadRun));
                readThread.Name = "Meter Read Thread";
                readThread.IsBackground = true;
                readThread.Start();
            }
        }
        private void ReadRun()
        {
            while (true)
            {
                if (sysStatus.IsStartRead)
                {
                    ReadFun();
                }
                else
                {
                    Thread.Sleep(30);
                }
            }
        }
        private void ReadFun()
        {
            lock (lockRead)
            {
                try
                {
                    //1. get this sation com port list

                    //2. open all port

                    //3. loop all port/all node read current data from one port,save to list

                    //4. do sleep 
                }
                catch (Exception ex)
                {
                    SystemLogger.Write(ex);
                }
            }
        }
        private void StartDoThread()
        {
            if (doProcessThread == null)
            {
                doProcessThread = new Thread(new ThreadStart(DoProcess));
                doProcessThread.Name = "Do Process Thread";
                doProcessThread.IsBackground = true;
                doProcessThread.Start();
            }
        }
        private void DoProcess()
        {
            while (true)
            {
                if (sysStatus.IsStartRead)
                {
                    ReadFun();
                }
                else
                {
                    Thread.Sleep(30);
                }
            }
        }
        private void DoProcessFun()
        {
            lock (lockDoProcess)
            {
                try
                {
                    //1. check list data

                    //2. get get one data

                    //3. analyse one data

                    //4. delete from list

                    //5. do next data
                }
                catch (Exception ex)
                {
                    SystemLogger.Write(ex);
                }
            }
        }
        private MeterSimulator()
        {
        }

        public bool Start()
        {
            throw new NotImplementedException();
        }

        public bool Stop()
        {
            throw new NotImplementedException();
        }

        public bool SetSerialPort(List<SerialPort> lstPort)
        {
            listCom = lstPort;
            return true;
        }
        public bool OpenPort()
        {
            bool ret = false;
            if (listCom.Count <= 0) return ret;
            ret = true;
            for (int i = 0; i < listCom.Count; i++)
            {
                try
                {
                    listCom[i].Open();
                }
                catch (Exception ex)
                {
                    ret = false;
                    SystemLogger.Write(LogType.Error, "Open serial port failed,com: "+listCom[i].PortName, "MeterSimulator");
                    continue;
                }
            }
            return ret;
        }


        public object AnalyseData(string data1, string type)
        {
            throw new NotImplementedException();
        }

        public string GetMeterID(string port, string node)
        {
            throw new NotImplementedException();
        }

        public bool SaveReal2DB(object data)
        {
            throw new NotImplementedException();
        }


        public bool ClosePort()
        {
            throw new NotImplementedException();
        }


        public void GetCommPort()
        {
            throw new NotImplementedException();
        }
    }
}
